﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.XepLoaiTongKetService;
namespace eTMS.Controllers{
public class XepLoaiTongKetControllerBase : CommonController{
	public XepLoaiTongKetControllerBase()
		: base()
	{	
		this.dao = new XepLoaiTongKetServiceClient();
	}
	~XepLoaiTongKetControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private XepLoaiTongKetEntity oXepLoaiTongKet;	
	public XepLoaiTongKetEntity XepLoaiTongKetObject{
		get{
			return oXepLoaiTongKet;
		}
		set{
			oXepLoaiTongKet=value;
		}
	}
	protected XepLoaiTongKetServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSXepLoaiTongKet(false);			
			return _Table;
		}
	}
	public void LoadAllDSXepLoaiTongKet(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oXepLoaiTongKet==null)return false;
		bool result=true;
		try{
			oXepLoaiTongKet = dao.SaveRet(oXepLoaiTongKet);
		}
		catch{
			result=false;
		}
		if(result){
			_ListXepLoaiTongKet.Add(oXepLoaiTongKet);
		}
		return result;
	}
	public bool Update(){
		if(oXepLoaiTongKet==null)return false;
		return dao.Update(oXepLoaiTongKet);
	}
	public bool Delete(){
		if(oXepLoaiTongKet==null)return false;
		bool retvalue = dao.Delete(oXepLoaiTongKet);
		if(retvalue){			
			_ListXepLoaiTongKet.Remove(oXepLoaiTongKet);
		}
		return retvalue;
	}
	public bool DeleteByID(String MaXepLoaiTongKet,Int32 ThangDanhGia_ID){
		bool retvalue = dao.DeleteByID(MaXepLoaiTongKet,ThangDanhGia_ID);		
		if(retvalue){
			XepLoaiTongKetEntity oDeleted=FindXepLoaiTongKet(MaXepLoaiTongKet,ThangDanhGia_ID);
			if(oDeleted!=null){
				_ListXepLoaiTongKet.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oXepLoaiTongKet = new XepLoaiTongKetEntity();
	}
    //Copy data from data row in grid to object
    public XepLoaiTongKetEntity MapDataRowToObject(DataRow row){
		XepLoaiTongKetEntity o = new XepLoaiTongKetEntity();
		 o.MaXepLoaiTongKet=Convert.ToString(row["MaXepLoaiTongKet"]);
		 if(!row.IsNull("TuDiem"))
			o.TuDiem=ConvertNumber(row["TuDiem"]);
		 if(!row.IsNull("DenDiem"))
			o.DenDiem=ConvertNumber(row["DenDiem"]);
		 if(!row.IsNull("IsBangCanTren"))
			o.IsBangCanTren=ConvertBool(row["IsBangCanTren"]);
		 if(!row.IsNull("IsBangCanDuoi"))
			o.IsBangCanDuoi=ConvertBool(row["IsBangCanDuoi"]);
		 if(!row.IsNull("ThangDanhGia_ID"))
			o.ThangDanhGia_ID=(Int32)(row["ThangDanhGia_ID"]);
		 if(!row.IsNull("MaXepLoaiTongKet"))
		    o.MaXepLoaiTongKet=(String)(row["MaXepLoaiTongKet"]);
		 if(!row.IsNull("ThangDanhGia_ID"))
		    o.ThangDanhGia_ID=(Int32)(row["ThangDanhGia_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(XepLoaiTongKetEntity o,DataRow row){
		row["MaXepLoaiTongKet"]=o.MaXepLoaiTongKet;
		row["TuDiem"]=o.TuDiem;
		row["DenDiem"]=o.DenDiem;
		row["IsBangCanTren"]=o.IsBangCanTren;
		row["IsBangCanDuoi"]=o.IsBangCanDuoi;
		row["ThangDanhGia_ID"]=o.ThangDanhGia_ID;
		row["MaXepLoaiTongKet"]=o.MaXepLoaiTongKet;
		row["ThangDanhGia_ID"]=o.ThangDanhGia_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.XepLoaiTongKetObject,row);
    }
	public void LoadObject(String MaXepLoaiTongKet,Int32 ThangDanhGia_ID){
		XepLoaiTongKetEntity oLoad=FindXepLoaiTongKet(MaXepLoaiTongKet,ThangDanhGia_ID);
		if(oLoad==null){
			oXepLoaiTongKet=this.LoadXepLoaiTongKet(MaXepLoaiTongKet,ThangDanhGia_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oXepLoaiTongKet.IsNullMaXepLoaiTongKet;
			 bNotIsNull&=oXepLoaiTongKet.IsNullThangDanhGia_ID;
			if(!bNotIsNull){
				this.ListXepLoaiTongKet.Add(oXepLoaiTongKet);
			}
		}
		else{
			oXepLoaiTongKet=oLoad;
		}
	}
	#region CachedList XepLoaiTongKet
	private static List<XepLoaiTongKetEntity> _ListXepLoaiTongKet=new List<XepLoaiTongKetEntity>();
    public List<XepLoaiTongKetEntity>ListXepLoaiTongKet{
		get{
			return _ListXepLoaiTongKet;
		}
	}
	public static List<XepLoaiTongKetEntity>ListXepLoaiTongKetStatic{
		get{
			return _ListXepLoaiTongKet;
		}
	}
	public XepLoaiTongKetEntity FindXepLoaiTongKet(String MaXepLoaiTongKet,Int32 ThangDanhGia_ID){
		XepLoaiTongKetEntity oResult=this.ListXepLoaiTongKet.Find(delegate(XepLoaiTongKetEntity oFind){
			bool result=true; 
			result&=oFind.MaXepLoaiTongKet.Equals(MaXepLoaiTongKet);result&=oFind.ThangDanhGia_ID.Equals(ThangDanhGia_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllXepLoaiTongKet(bool clearCache){
		if(_ListXepLoaiTongKet.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		XepLoaiTongKetServiceClient dao = new XepLoaiTongKetServiceClient();
		_ListXepLoaiTongKet=new List<XepLoaiTongKetEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public XepLoaiTongKetEntity LoadXepLoaiTongKet(String MaXepLoaiTongKet,Int32 ThangDanhGia_ID){			
		XepLoaiTongKetEntity o=dao.SelectOne(MaXepLoaiTongKet,ThangDanhGia_ID);		
		return o;
	}
	#endregion
}
}
